package com.sky.service.impl;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.Orders;
import com.sky.mapper.AdminOrderMapper;
import com.sky.mapper.OrderMapper;
import com.sky.service.AdminOrderService;
import com.sky.vo.OrderStatisticsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class AdminOrderServicelmpl implements AdminOrderService {
    @Autowired
    private AdminOrderMapper adminOrderMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserServicelmpl userServicelmpl;


    /**
     * 查询不同状态的订单的数量
     * @return
     */
    @Override
    public OrderStatisticsVO statis() {
        //获得每种订单状态的数量
        Integer comfirmed=adminOrderMapper.countStatis(3);
        Integer deliveryInProgress=adminOrderMapper.countStatis(4);
        Integer toBeConfirmed=adminOrderMapper.countStatis(2);
        //把数量赋值到VO并返回
        OrderStatisticsVO orderStatisticsVO=new OrderStatisticsVO();
        orderStatisticsVO.setConfirmed(comfirmed);
        orderStatisticsVO.setToBeConfirmed(toBeConfirmed);
        orderStatisticsVO.setDeliveryInProgress(deliveryInProgress);

        return orderStatisticsVO;
    }

    /**
     * 根据订单id查询订单明细
     * @param id
     * @return
     */
    @Override
    public Orders details(Long id) {
        Orders orders=adminOrderMapper.details(id);
        return orders;
    }

    /**
     * 取消订单 更改订单状态  更新-订单取消原因
     * @param ordersCancelDTO
     */
    @Override
    public void cancel(OrdersCancelDTO ordersCancelDTO) {
        Orders orders= adminOrderMapper.details(ordersCancelDTO.getId());
        orders.setCancelReason(ordersCancelDTO.getCancelReason());
        orders.setStatus(6);
        orders.setCancelTime(LocalDateTime.now());

        adminOrderMapper.cancel(orders);
    }

    @Override
    public void complete(Integer id) {
        Orders orders= adminOrderMapper.details(Long.valueOf(id));
        orders.setStatus(5);
        orders.setDeliveryTime(LocalDateTime.now());
        adminOrderMapper.complete(orders);
    }

    /**
     * 拒单
     * @param ordersRejectionDTO
     */
    @Override
    public void reject(OrdersRejectionDTO ordersRejectionDTO) {
        Orders orders= adminOrderMapper.details(ordersRejectionDTO.getId());
        orders.setStatus(6);
        orders.setRejectionReason(ordersRejectionDTO.getRejectionReason());
        orders.setCancelTime(LocalDateTime.now());
        adminOrderMapper.reject(orders);

    }

    /**
     * 接单
     * @param id
     */
    @Override
    public void confirm(Integer id) {
        Orders orders= adminOrderMapper.details(Long.valueOf(id));
        orders.setStatus(3);
        adminOrderMapper.confirm(orders);
    }

    @Override
    public void delivery(Integer id) {
        Orders orders= adminOrderMapper.details(Long.valueOf(id));
        orders.setStatus(4);
        adminOrderMapper.delivery(orders);

    }


}
